package vboot.app.module.de.supp.cate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import vboot.core.common.api.R;
import vboot.core.common.dao.Sqler;
import vboot.core.common.pojo.Ztree;

import java.util.List;

//供应商分类接口
@RestController
@RequestMapping("de/supp/cate")
public class DeSuppCateApi {

    private String table = "de_supp_cate";

    //获取除自身及子节点外的分类
    @GetMapping("treez")
    public R getTreez(String name, String id) {
        List<Ztree> list = service.tier2Choose(table, id, name);
        return R.ok(list);
    }

    //获取所有分类-用于分类选择下拉框
    @GetMapping("treea")
    public R getTreea(String name) {
        List<Ztree> list = service.findTreeList(table, name);
        return R.ok(list);
    }

    //获取分类treeTable数据
    @GetMapping("tree")
    public R getTree(String name) {
        Sqler sqler = new Sqler(table);
        sqler.addLike("t.name", name);
        sqler.addOrder("t.ornum");
        sqler.addWhere("t.avtag=1");
        sqler.addSelect("t.crtim,t.uptim,t.pid,t.notes");
        List<DeSuppCate> list = service.findTree(sqler);
        return R.ok(list);
    }

    //获取分类分页数据
    @GetMapping
    public R get(String name) {
        Sqler sqler = new Sqler(table);
        sqler.addLike("t.name", name);
        sqler.addSelect("t.notes");
        return R.ok(service.findPageData(sqler));
    }

    //获取单个分类详细信息
    @GetMapping("one/{id}")
    public R getOne(@PathVariable String id) {
        return R.ok(service.findOne(id));
    }

    //新增供应商分类
    @PostMapping
    public R post(@RequestBody DeSuppCate main) {
        return R.ok(service.insert(main));
    }

    //更新供应商分类
    @PutMapping
    public R put(@RequestBody DeSuppCate main) {
        return R.ok(service.update(main, table));
    }

    //删除供应商分类
    @DeleteMapping("{ids}")
    public R delete(@PathVariable String[] ids) {
        return R.ok(service.delete(ids));
    }

    @Autowired
    private DeSuppCateService service;

}
